{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import time\n",
    "import os\n",
    "import requests\n",
    "\n",
    "import fml_manager\n",
    "from fml_manager import *\n",
    "\n",
    "manager = fml_manager.FMLManager()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## For more details about the FMLManager, please refer to this [document](https://kubefate.readthedocs.io/README.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "manager = fml_manager.FMLManager()\n",
    "response = manager.load_data(url='./data/breast_b.csv', namespace='fate_flow_test_breast', table_name='breast_b', work_mode=1, head=1, partition=10)\n",
    "output = json.loads(response.content)\n",
    "print(output)\n",
    "guest_job_id = output['jobId']\n",
    "guest_query_condition = QueryCondition(job_id=guest_job_id)\n",
    "\n",
    "manager.query_job_status(guest_query_condition, max_tries=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# Pipeline\n",
    "data_io = ComponentBuilder(name='dataio_0',\n",
    "                           module='DataIO')\\\n",
    "                           .add_input_data('args.train_data')\\\n",
    "                           .add_output_data('train')\\\n",
    "                           .add_output_model('dataio').build()\n",
    "        \n",
    "hetero_feature_binning = ComponentBuilder(name='hetero_feature_binning_0',\n",
    "                                          module='HeteroFeatureBinning')\\\n",
    "                                          .add_input_data('dataio_0.train')\\\n",
    "                                          .add_output_data('train')\\\n",
    "                                          .add_output_model('hetero_feature_binning').build()\n",
    "\n",
    "hetero_feature_selection = ComponentBuilder(name='hetero_feature_selection_0',\n",
    "                                            module='HeteroFeatureSelection')\\\n",
    "                                            .add_input_data('hetero_feature_binning_0.train')\\\n",
    "                                            .add_input_isometric_model('hetero_feature_binning_0.hetero_feature_binning')\\\n",
    "                                            .add_output_data('train')\\\n",
    "                                            .add_output_model('selected').build()\n",
    "\n",
    "hetero_lr = ComponentBuilder(name='hetero_lr_0',\n",
    "                             module='HeteroLR')\\\n",
    "                             .add_input_train_data('hetero_feature_selection_0.train')\\\n",
    "                             .add_output_data('train')\\\n",
    "                             .add_output_model('hetero_lr').build()\n",
    "\n",
    "evaluation = ComponentBuilder(name='evaluation_0',\n",
    "                              module='Evaluation',\n",
    "                              need_deploy=False)\\\n",
    "                              .add_input_data('hetero_lr_0.train')\\\n",
    "                              .add_output_data('evaluate').build()\n",
    "pipeline = Pipeline(\n",
    "    data_io, \n",
    "    hetero_feature_selection,  \n",
    "    hetero_feature_binning, \n",
    "    hetero_lr, \n",
    "    evaluation)\n",
    "\n",
    "# Configuration\n",
    "\n",
    "initiator = Initiator(role='guest', party_id=10000)\n",
    "\n",
    "job_parameters = JobParameters(work_mode=1)\n",
    "\n",
    "role = RoleBuilder()\\\n",
    "    .add_guest(party_id=10000)\\\n",
    "    .add_host(party_id=9999)\\\n",
    "    .add_arbiter(party_id=9999).build()\n",
    "\n",
    "guest_data_io_config = {\n",
    "    'with_label': [True],\n",
    "    'label_name': ['y'],\n",
    "    'label_type': ['int'],\n",
    "    'output_format': ['dense']\n",
    "}\n",
    "\n",
    "host_data_io_config = {\n",
    "    'with_label': [False],\n",
    "    'output_format': ['dense']\n",
    "}\n",
    "\n",
    "role_parameters = RoleParametersBuilder()\\\n",
    "    .add_guest_train_data(namespace='fate_flow_test_breast', name='breast_b')\\\n",
    "    .add_guest_module_config(module='dataio_0', config=guest_data_io_config)\\\n",
    "    .add_host_train_data(namespace='fate_flow_test_breast', name='breast_a')\\\n",
    "    .add_host_module_config(module='dataio_0', config=host_data_io_config).build()\n",
    "\n",
    "hetero_lr_params = {\n",
    "    'penalty': 'L2',\n",
    "    'optimizer': 'rmsprop',\n",
    "    #'eps': 1e-5,\n",
    "    'alpha': 0.01,\n",
    "    'max_iter': 3,\n",
    "    #'converge_func': 'diff',\n",
    "    'batch_size': 320,\n",
    "    'learning_rate': 0.15,\n",
    "    'init_param': {\n",
    "        'init_method': 'random_uniform'\n",
    "    }\n",
    "}\n",
    "\n",
    "algorithm_parameters = AlgorithmParametersBuilder()\\\n",
    "    .add_module_config(module='hetero_lr_0', config=hetero_lr_params).build()\n",
    "\n",
    "config = Config(initiator, job_parameters, role, role_parameters, algorithm_parameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Click [here](/fateboard-10000/) to view jobs in FATE Board"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = manager.submit_job(pipeline.to_dict(),config.to_dict())\n",
    "manager.prettify(response, verbose=True)\n",
    "stdout = json.loads(response.content)\n",
    "print(stdout)\n",
    "if stdout[\"retcode\"] ==100:\n",
    "    pass\n",
    "else:\n",
    "    job_id = stdout['jobId']\n",
    "    query_condition = QueryCondition(job_id)\n",
    "    model_id, model_version = '', ''\n",
    "    manager.query_job_status(query_condition, max_tries=20)\n",
    "\n",
    "    manager.prettify(response, verbose=True)\n",
    "    output = json.loads(response.content)\n",
    "    model_id, model_version = output['data']['model_info']['model_id'], output['data']['model_info']['model_version']\n",
    "    # data is a pandas.DataFrame\n",
    "    print(job_id)\n",
    "    manager = fml_manager.FMLManager()\n",
    "    data = manager.track_component_output_data(job_id=job_id, role='guest', party_id='10000', component_name='hetero_lr_0')\n",
    "    #print(data)\n",
    "    print(data.head(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#wait till traing finished\n",
    "print(job_id)\n",
    "data = manager.track_component_output_data(job_id=job_id, role='guest', party_id='10000', component_name='hetero_lr_0')\n",
    "print('data')\n",
    "print(data.head(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
